package com.oceansoft.core.jxkh.controller;

import com.hui.platform.system.exception.HuiException;
import com.oceansoft.core.common.constant.Constant;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.Message;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.jxkh.domain.Personnel;
import com.oceansoft.core.jxkh.service.IPersonnelService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by OS on 2017/12/5.
 */

@Controller
@RequestMapping("/personnel")
public class PersonnelController {

    @Resource
    private IPersonnelService personnelService;

    @RequestMapping(value="allPersonnelInfo.json")
    public void allPersonnelInfo(HttpServletRequest request, ModelMap modelMap){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        HashMap<String, String> paramap = new HashMap<String, String>();
        paramap.put("offset",offset);
        paramap.put("limit",limit);
        paramap.put("order",order);
        Pagination userPag = personnelService.findAllPersonnel(paramap);
        modelMap.addAttribute("rows",userPag.getList());
        modelMap.addAttribute("total",userPag.getTotalRows());
    }

    @RequestMapping(value = "findPersonnelInfoByDeptId.json")
    public void findPersonnelInfoByDeptId(ModelMap modelMap,HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        String deptId = request.getParameter("deptId");
        HashMap<String,String> map = new HashMap<>();
        map.put("deptId",deptId);
        map.put("offset",offset);
        map.put("limit",limit);
        map.put("order",order);
        Pagination userPag = personnelService.findPersonnelInfoByDeptId(map);
        modelMap.addAttribute("rows",userPag.getList());
        modelMap.addAttribute("total",userPag.getTotalRows());
    }

    @RequestMapping(value = "editDeptManager.json")
    public void editDeptManager(ModelMap modelMap,int dbid,int deptId){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            personnelService.editDeptManagerToNo(deptId);
            personnelService.editDeptManagerByDbidToYes(dbid);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="editPersonnelInfo",method = RequestMethod.POST)
    public void editPersonnelInfo(HttpServletRequest request, ModelMap modelMap,SysUser sysUser){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            personnelService.editPersonnelInfo(sysUser);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="findPersonnelInfo.html")
    public String findPersonnelInfo(int dbid,ModelMap modelMap){
        SysUser sysUser = personnelService.findPersonnelInfoByDbid(dbid);
        modelMap.addAttribute("sysUser",sysUser);
        return "jump/rygl_bj";
    }

    @RequestMapping(value="delPersonInfo.json")
    public void delPersonInfo(int dbid,ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            personnelService.deletePersonnelInfoByDbid(dbid);
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }

    @RequestMapping(value="editJoinCheck.json")
    public void editJoinCheck(int dbid,ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            if (personnelService.findJoinCheck(dbid)==1) {
                personnelService.editJoinCheck(dbid,0);
            }else{
                personnelService.editJoinCheck(dbid,1);
            }
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }
    @RequestMapping(value="editActiveFlag.json")
    public void editActiveFlag(int dbid,ModelMap modelMap){
        Message message = new Message("操作成功");
        message.setStatus(Constant.RESULT_SUCC);
        try {
            if (personnelService.findActiveFlag(dbid)==1) {
                personnelService.editActiveFlag(dbid,0);
            }else{
                personnelService.editActiveFlag(dbid,1);
            }
        }catch (Exception e){
            e.printStackTrace();
            message.setStatus(Constant.RESULT_FAILD);
            message.setBody(e.getMessage());
        }
        modelMap.addAttribute("data",message);
    }
}
